package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcNursingLog;
import com.csun.cmny.provider.model.dto.NursingLogDto;
import com.csun.cmny.provider.model.dto.NursingLogQueryDto;
import com.csun.cmny.provider.model.vo.NursinglogDetailVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcNursingLogService;
import com.csun.cmny.provider.utils.NursinglogUtil;
import com.csun.cmny.provider.validator.NursinglogSaveValid;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/nursinglog", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcNursingLogController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcNursingLogService omcNursingLogService;

    /**
     * 护理日志-查询
     * @param: [nursingLogQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/selectByDate")
    public Wrapper queryByDate(@RequestBody NursingLogQueryDto nursingLogQueryDto) throws ParseException {

        OmcNursingLog omcNursingLog = omcNursingLogService.queryByDate(nursingLogQueryDto,getLoginAuthDto());

        if(omcNursingLog != null){
            //获取膳食服务集合
            String meal = omcNursingLog.getMeal();
            if(!(meal == null || meal.length() == 0)){
                List<NursinglogDetailVo> mealList = NursinglogUtil.setMealList(meal);
                omcNursingLog.setMealList(mealList);
            }else {
                omcNursingLog.setMealList(new ArrayList<NursinglogDetailVo>());
            }
            //获取消毒服务集合
            String disinfection = omcNursingLog.getDisinfection();
            if(!(disinfection == null || disinfection.length() == 0)){
                List<NursinglogDetailVo> disinfectionList = NursinglogUtil.setDisinfectionList(disinfection);
                omcNursingLog.setDisinfectionList(disinfectionList);
            }else{
                omcNursingLog.setDisinfectionList(new ArrayList<NursinglogDetailVo>());
            }
            //获取基础护理记录集合
            String basicnursing = omcNursingLog.getBasicNursing();
            if(!(basicnursing == null || basicnursing.length() == 0)){
                List<NursinglogDetailVo> basicnursingList = NursinglogUtil.setInfoList(basicnursing);
                omcNursingLog.setBasicnursingList(basicnursingList);
            }else {
                omcNursingLog.setBasicnursingList(new ArrayList<NursinglogDetailVo>());
            }
            //获取口腔护理记录集合
            String mouthcare = omcNursingLog.getMouthCare();
            if(!(mouthcare == null || mouthcare.length() == 0)){
                List<NursinglogDetailVo> mouthcareList = NursinglogUtil.setInfoList(mouthcare);
                omcNursingLog.setMouthcareList(mouthcareList);
            }else {
                omcNursingLog.setMouthcareList(new ArrayList<NursinglogDetailVo>());
            }
            //获取褥疮护理记录集合
            String bedsorecare = omcNursingLog.getBedsoreCare();
            if(!(bedsorecare == null || bedsorecare.length() == 0)){
                List<NursinglogDetailVo> bedsorecareList = NursinglogUtil.setInfoList(bedsorecare);
                omcNursingLog.setBedsorecareList(bedsorecareList);
            }else {
                omcNursingLog.setBedsorecareList(new ArrayList<NursinglogDetailVo>());
            }
            //获取康复训练记录集合
            String recoverytrain = omcNursingLog.getRecoveryTrain();
            if(!(recoverytrain == null || recoverytrain.length() == 0)){
                List<NursinglogDetailVo> recoverytrainList = NursinglogUtil.getRecoveryList(recoverytrain);
                omcNursingLog.setRecoverytrainList(recoverytrainList);
            }else{
                omcNursingLog.setRecoverytrainList(new ArrayList<NursinglogDetailVo>());
            }
        }

//        return WrapMapper.ok(omcNursingLog);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), omcNursingLog);
    }

    /*
    * 查询当天菜单
    *
    * @param: [nursingLogQueryDto]
    * @return: com.csun.cmny.util.wrapper.Wrapper
    */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/selectMenus")
    public Wrapper selectMenus(@RequestBody NursingLogQueryDto nursingLogQueryDto) {

        List<OmcNursingLog> result = omcNursingLogService.selectMenus(nursingLogQueryDto,getLoginAuthDto());

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 查询选择了压疮护理的老人集合
     * @param: [nursingLogQueryDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/querySoreCareOldList")
    public Wrapper querySoreCareOldList(@RequestBody NursingLogQueryDto nursingLogQueryDto) {

        List<OmcNursingLog> result = omcNursingLogService.querySoreCareOldList(nursingLogQueryDto,getLoginAuthDto());

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 消毒服务信息-保存
     *
     * @param: [nursingLogDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/disinfectSave")
    public Wrapper disinfectAdd(@Validated(NursinglogSaveValid.class) @RequestBody NursingLogDto nursingLogDto) {

        int ret = save(nursingLogDto);

        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 基础护理信息-保存
     *
     * @param: [nursingLogDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/basicCareSave")
    public Wrapper basicCareSave(@Validated(NursinglogSaveValid.class) @RequestBody NursingLogDto nursingLogDto) {

        int ret = save(nursingLogDto);

        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 口腔护理信息-保存
     *
     * @param: [nursingLogDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/mouthCareSave")
    public Wrapper mouthCareSave(@Validated(NursinglogSaveValid.class)@RequestBody NursingLogDto nursingLogDto) {

        int ret = save(nursingLogDto);

        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 压疮护理信息-保存
     *
     * @param: [nursingLogDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/soreCareSave")
    public Wrapper soreCareSave(@Validated(NursinglogSaveValid.class) @RequestBody NursingLogDto nursingLogDto) {

        int ret = save(nursingLogDto);

        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }


    /**
     * 康复训练信息-保存
     *
     * @param: [nursingLogDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/recoveryTrainSave")
    public Wrapper recoveryTrainSave(@Validated(NursinglogSaveValid.class) @RequestBody NursingLogDto nursingLogDto) {

        int ret = save(nursingLogDto);
        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 膳食服务信息-保存
     *
     * @param: [nursingLogDto]
     * @return: com.csun.cmny.util.wrapper.Wrapper
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/mealServerSave")
    public Wrapper mealServerSave(@Validated(NursinglogSaveValid.class) @RequestBody NursingLogDto nursingLogDto) {

        int ret = save(nursingLogDto);

        if (ret < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }
//        return WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /**
     * 护理日志-保存
     *
     * @param: [nursingLogDto]
     * @return: java.lang.Integer
     */
    public Integer save(NursingLogDto nursingLogDto){
        OmcNursingLog omcNursingLog = new OmcNursingLog();
        BeanUtils.copyProperties(nursingLogDto,omcNursingLog);
        return omcNursingLogService.saveNursingLog(omcNursingLog,getLoginAuthDto());
    }

    /*
    * 护理日志-清洗
    *
    * */
    @PostMapping(value = "/dccNursingLog")
    public Wrapper<Integer> dccNursingLog(){
        int ret = omcNursingLogService.dccNursingLog();
//        return  WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }

    /*
    * 消毒服务-清洗
    * */
    @PostMapping(value = "/dccDisinfection")
    public Wrapper<Integer> dccDisinfection(){
        int ret = omcNursingLogService.dccDisinfection();
//        return  WrapMapper.ok(ret);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), ret);
    }
}
